package vistas;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.*;

import entidades.Casillas_correo;

import rmi.InterfazRemota;

public class MDIChildCorreoAbrir extends JInternalFrame {
	
	private String idMensaje;
	private JLabel lblDe;
	private JLabel txtDe;
	private JLabel lblPara;
	private JComboBox cmbPara;
	
	private InterfazRemota objetoRemoto;
	private Vector<Casillas_correo> vectorCasillas;
	private int id_casilla;
	private Vector<String> vectorPara;
	
	private JLabel lblAsunto;
	private JLabel txtAsunto;
	private JLabel lblEstado;
	private JLabel txtEstado;
	private JLabel lblPrioridad;
	private JLabel txtPrioridad;
	private JLabel lblFecha;
	private JLabel txtFecha;
	
	private JButton btnEnviar;
	private JButton btnCancelar;
	private JTextArea txtArea;
	private Vector<String>	datos;
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int SEPARACION = 24;
	static int inset = 0;
	private MDI mdi;
	

    public MDIChildCorreoAbrir(MDI mdi) {
        super(null,true,true,true,true);
        this.mdi = mdi;
        objetoRemoto = mdi.getObjetoRemoto();
        this.id_casilla = mdi.getId_casilla();
        
        inicializar();
        this.datos = mdi.getDatos();
        idMensaje= datos.elementAt(0);
        txtDe.setText(datos.elementAt(1));
        //cmbPara.setText(datos.elementAt(2));
        txtAsunto.setText(datos.elementAt(3));
        txtArea.setText(datos.elementAt(4));
        txtFecha.setText(datos.elementAt(5));
        txtEstado.setText(datos.elementAt(6));
        txtPrioridad.setText(datos.elementAt(7));
        
        
        setLocation(SEPARACION*inset, SEPARACION*inset);
        ++inset;
    }
    
    
    private void inicializar() {
        setName("mdiChild");
        vectorPara = new Vector<String>();
        
        lblDe =  new JLabel("De: ");
        txtDe = new JLabel();
        
        try {
			
        	vectorCasillas = objetoRemoto.listarCasillas();
	        for (int i=0;i<vectorCasillas.size();i++){
	        	vectorPara.add(vectorCasillas.elementAt(i).getCasilla());
	        	if (vectorCasillas.elementAt(i).getId_casilla()==id_casilla){
	        		txtDe.setText(vectorCasillas.elementAt(i).getCasilla());
	        	}
	        }

	        
	        
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        lblPara =  new JLabel("Para: ");
        cmbPara = new JComboBox(vectorPara);
        

        
        lblAsunto = new JLabel("Asunto");
        txtAsunto = new JLabel();
        lblEstado = new JLabel("Estado");
        txtEstado = new JLabel();
        lblPrioridad = new JLabel("Prioridad");
        txtPrioridad = new JLabel();
        lblFecha = new JLabel("Fecha");
        txtFecha = new JLabel();
        
        
        
        txtArea = new JTextArea("Test de cuerpo de mail",4,1);
        btnEnviar = new JButton("Enviar");
        btnEnviar.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					objetoRemoto.enviarCorreoGuardado(idMensaje.toString(),txtDe.getText(), cmbPara.getSelectedItem().toString(), txtAsunto.getText(), txtArea.getText(), txtFecha.getText(), "Enviado", txtPrioridad.getText());
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
        	
        });
        btnCancelar = new JButton("Cancelar");
        btnCancelar.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
								
			}
        	
        });
        
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,1));
        panel2.add(txtArea);
        
        Panel panel = new Panel();
        panel.setLayout(new GridLayout(7,2));
        panel.add(lblDe);
        panel.add(txtDe);
        panel.add(lblPara);
        panel.add(cmbPara);
        panel.add(lblAsunto);
        panel.add(txtAsunto);
        panel.add(lblEstado);
        panel.add(txtEstado);
        panel.add(lblPrioridad);
        panel.add(txtPrioridad);
        panel.add(lblFecha);
        panel.add(txtFecha);
        panel.add(btnCancelar);
        panel.add(btnEnviar);
        
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(panel,BorderLayout.PAGE_START);
        container.add(panel2,BorderLayout.CENTER);
        
        int chWidth = (int) mdi.getWidth()*55/100;
        int chHeight = (int) mdi.getHeight()*50/100;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-chWidth)/2, (screenSize.height-chHeight)/2, chWidth, chHeight);
     }

}
